//
//  DatePickView.swift
//  WineComing
//
//  Created by xiaozhi on 15/7/12.
//  Copyright (c) 2015年 xiaozhi. All rights reserved.
//

import UIKit
enum DateType{
    case Date,time
}
class DatePickView: UIView {
    @IBOutlet var datePicker:UIDatePicker!
    @IBOutlet var cancelBtn:UIButton!
    @IBOutlet var okBtn:UIButton!
    @IBOutlet var bgView:UIView!
    @IBAction func dismissSelf(){
        didSetDate!(dateFormatter.stringFromDate(datePicker.date))
        self.removeFromSuperview()
    }
    @IBAction func cancelBtnClick(){
        self.removeFromSuperview()
    }
    @IBAction func okBtnClick(){
        didSetDate!(dateFormatter.stringFromDate(datePicker.date))
        self.removeFromSuperview()
    }
    let dateFormatter:NSDateFormatter!
    var type:DateType = .Date{
        didSet{
            switch self.type{
            case DateType.Date:
                datePicker.datePickerMode = UIDatePickerMode.Date
                dateFormatter.dateFormat = "MM-dd"
            case DateType.time:
                datePicker.datePickerMode = UIDatePickerMode.Time
                dateFormatter.dateFormat = "HH:mm"
                
            default:break
            }
        }
    }
    var didSetDate:((String)->Void)?
    override func awakeFromNib() {
        super.awakeFromNib()
        bgView.layer.shadowColor = UIColor.lightGrayColor().CGColor
        bgView.layer.shadowOffset = CGSizeMake(1, 2)
        bgView.layer.shadowOpacity = 0.5
        okBtn.layer.cornerRadius = 4.0
        cancelBtn.layer.cornerRadius = 4.0
        datePicker.minimumDate = NSDate(timeIntervalSinceNow:30.0*60.0)
        datePicker.minuteInterval = 10*60
        datePicker.timeZone = NSTimeZone(name: "Asia/beijing")
        datePicker.backgroundColor = UIColor.whiteColor()
        datePicker.date = datePicker.minimumDate!
        let local = NSLocale.currentLocale()
        datePicker.locale = local
    }
    required init?(coder aDecoder: NSCoder) {
        dateFormatter = NSDateFormatter()
        super.init(coder: aDecoder);
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
